package controller

import (
	"github.com/doujunyu/gogo/job"
	"github.com/doujunyu/gogo/sql_aid"
	"go_self/model"
	"go_self/server"
	"strconv"
)

// OrderByPlaceOrder 预定订单详情
func OrderByPlaceOrder(j *job.Job) {
	if j.Input["sku_json"] == "" {
		j.JsonError(nil, "请先选择商品")
		return
	}
	//sku生成的组合数据
	data, err := server.OrderBySkuJsonToData(j.Input["sku_json"])
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}

	//收货地址
	userAddressSql, arge := sql_aid.PgTable(model.TableByUserAddress).Where("user_id = ?", j.GroupData["user_id"]).OrderBy("is_default desc,time_u desc").PageSize("1", "2").ToSql()
	userAddress, err := sql_aid.DataToMap(model.PgSql.Query(userAddressSql, arge...))
	if err != nil || len(userAddress) > 0 {
		data["user_address"] = userAddress[0]
	} else {
		data["user_address"] = nil
	}

	j.JsonSuccess(data)
	return

}

// OrderByCreateOrder 订单生成
func OrderByCreateOrder(j *job.Job) {
	input := make(map[string]string)
	input["sku_json"] = j.Input["sku_json"]
	input["address_user"] = j.Input["address_user"]
	input["address_mobile"] = j.Input["address_mobile"]
	input["address_info"] = j.Input["address_info"]
	input["pay_type"] = j.Input["pay_type"]
	data, err := server.OrderByCreateOrder(j.GroupData["user_id"].(int64), input)
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}
	j.JsonSuccess(data)
	return
}

// OrderByList 订单列表
func OrderByList(j *job.Job) {
	input := make(map[string]string)
	input["user_id"] = strconv.FormatInt(j.GroupData["user_id"].(int64), 10)
	input["status"] = j.Input["status"]
	input["page"] = j.Input["page"]
	input["page_size"] = j.Input["page_size"]

	data := server.OrderByList(input)
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// OrderByToWaitDiscuss 收货后状态变为待评价状态
func OrderByToWaitDiscuss(j *job.Job) {
	if j.Input["order_child_id"] == "" {
		j.JsonError(nil, "参数错误")
		return
	}
	OrderChildId, _ := strconv.ParseInt(j.Input["order_child_id"], 10, 64)
	err := server.OrderByToWaitDiscuss(j.GroupData["user_id"].(int64), OrderChildId)
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}
	j.JsonSuccess()
	return

}

// OrderByToFinish 订单评价
func OrderByToFinish(j *job.Job){
	input := make(map[string]string)
	input["order_child_id"] = j.Input["order_child_id"]
	input["content"] = j.Input["content"]
	input["content_picture"] = j.Input["content_picture"]
	input["star_grade"] = j.Input["star_grade"]
	err := server.OrderByToFinish(j.GroupData["user_id"].(int64),input)
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}
	j.JsonSuccess()
	return
}



